package com.zmx.user.web;

import com.zmx.common.common.web.AbstractController;
import com.zmx.common.common.web.rest.ResponseResult;
import com.zmx.user.dto.MemberCommentsDto;
import com.zmx.user.service.MemberCommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 用户评价控制器
 * @ClassName: MemberCommentsController
 * @Author zhaomxr
 * @Date 2022-01-12 11:12
 */
@Api("用户评价")
@Slf4j
@RestController
@RequestMapping("comment")
public class MemberCommentsController extends AbstractController<MemberCommentsDto, MemberCommentsService> {

    @Autowired
    private MemberCommentsService memberCommentsService;

    @ApiOperation(value = "分页获取评论", tags = "分页获取评论")
    @GetMapping(value = "getCommentsByPage")
    public ResponseResult getCommentsByPage(@RequestParam(required = true) Long productId,
                                            @RequestParam(required = true) Integer pageIndex,
                                            @RequestParam(required = true) Integer pageSize) {
        try {
            Page page = memberCommentsService.getCommentsByPage(productId, pageIndex, pageSize);
            return ResponseResult.success(page);
        } catch (Exception e) {
            log.error("分页获取评论失败！", e);
            return ResponseResult.fail("分页获取评论失败");
        }
    }

    @ApiOperation(value = "新增商品评价", tags = "新增商品评价")
    @PostMapping(value = "addComments")
    public ResponseResult addComments(@RequestBody(required = true) MemberCommentsDto memberCommentsDto) {
        try {
            memberCommentsService.addComments(memberCommentsDto);
            return ResponseResult.success();
        } catch (Exception e) {
            log.error("新增商品评价失败！", e);
            return ResponseResult.fail("新增商品评价失败");
        }
    }
}
